package com.li.Iteratorpattern;

/**
 * @author lirenchun
 */
public class ContainerImpl implements Container {
    private String[] names;

    /**
     * 构造数组
     *
     * @param names
     */
    public ContainerImpl(String[] names) {
        this.names = names;
    }

    @Override
    public Iterator getIterator() {
        return new IteratorImpl();
    }

    // 内部类，实现了迭代器接口
    private class IteratorImpl implements Iterator {
        /**
         * 数组下标
         */
        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < names.length;
        }

        @Override
        public Object next() {
            if (this.hasNext()) {
                return names[index++];
            }else{
                return null;
            }
        }
    }
}
